/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package carrello.impl.boundary.registereduser;

import carrello.impl.controller.registereduser.ShoppingCartCTRL;
import carrello.impl.boundary.AbstractUI;
import carrello.model.entities.Product;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
/**
 *
 * @author mepcotterell
 */
public class ShoppingCartUI extends AbstractUI {
    Logger log = Logger.getLogger(ShoppingCartUI.class);
    private Map pmap;
    private int[] productid;
    private int[] quantity;
    private int check = 1;
    public void setproductid ( int[] p ) {
        productid = p;
    }
    public int[] getproductid () {
        return productid;
    }

    public int[] getQuantity() {
        return quantity;
    }

    public void setQuantity(int[] quantity) {
        this.quantity = quantity;
    }

    public Map<Product, Integer> getPmap() {
        return pmap;
    }

    public void setPmap(Map<Product, Integer> pmap) {
        this.pmap = pmap;
    }

    public int getCheck() {
        return check;
    }

    public void setCheck(int check) {
        this.check = check;
    }
    
    public String execute()
    { 
        if ( !getSessionIsLoggedIn()) {
            return LOGIN;
        } // if
        Map<Integer,Integer> cartMap = getSessionCart();
        if (getCartCount() == 0) {
            return ERROR;
        } else if(quantity != null) {
            setCheck(0);
            for ( int i = 0; i < quantity.length; i ++) {
                if (quantity[i] != 0) {
                    cartMap.put(productid[i], quantity[i]);
                    viewCart(productid[i], quantity[i]);
                } else {
                    cartMap.remove(productid[i]);
                }
            }
            setSessionCart(cartMap);
            if (getSessionCart().size() == 0) {
                return ERROR;
            } // end if
            return SUCCESS;
        } else {
            setCheck(1);
            Set<Map.Entry<Integer,Integer>> carte = cartMap.entrySet();
            for (Map.Entry<Integer, Integer> cart : carte) {
                viewCart(cart.getKey(), cart.getValue());
            } // for
        } // if
       return SUCCESS;
    } // execute
    public void viewCart(Integer productId, Integer quantity)
    {
        ShoppingCartCTRL shop_ctrl = new ShoppingCartCTRL();
        Map<Product, Integer> map = shop_ctrl.getProductInfo(productId, quantity);
        if (pmap == null) {
            pmap = new HashMap<Product, Integer>();
            pmap.putAll(map);
        } // if
        pmap.putAll(map);
    }
}
